//
//  W_MapViewController.m
//  Go!
//
//  Created by 凌風 on 16/7/14.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "W_MapViewController.h"
// 使用定位服务，必须引入 CoreLocation 框架
#import <CoreLocation/CoreLocation.h>
// 使用地图（MKMapView）,必须引入 MapKit 框架
#import <MapKit/MapKit.h>

@interface W_MapViewController () <CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager; /**< 定位服务 */
@property (nonatomic, strong) MKMapView *mapView; /**< 地图视图 */
@property (nonatomic, strong) UIButton *locationBtn; /**< 显示我的位置按钮 */
@property (nonatomic, strong) UIView *bottomView; /**< 底部商户地址视图 */
@property (nonatomic, strong) UILabel_Font *shopNameLabel;/**< 商户名称标签 */
@property (nonatomic, strong) UILabel_Font *shopAddressLabel;/**< 商户地址标签 */

@end

@implementation W_MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商家地图";
    
    [self initializeLoactionService];
    [self initializeInterface];
    
    
    // 设置经纬度
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    
//    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(30.67, 100.06);
    
    // 创建标注数据源
    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = coordinate;
    
    // 将标注数据源添加到地图
    [self.mapView addAnnotation:annotation];
    // 控制地图显示范围
    [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000) animated:true];
}


#pragma mark -- Initialize
/**
 *  初始化定位服务
 */
- (void)initializeLoactionService {
    
    // 初始化定位服务
    self.locationManager = [[CLLocationManager alloc] init];
    
    // 请求定位
    [self.locationManager requestAlwaysAuthorization];
    
    // 设置定位精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    // 设置定位频率
    self.locationManager.distanceFilter = 10.f;
    
    // 设置代理
    self.locationManager.delegate = self;
    
    
    
    
}

/**
 *  初始化用户界面
 */
- (void)initializeInterface {
    // 设置导航栏透明
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    // 关闭navigationBar自动布局
    self.automaticallyAdjustsScrollViewInsets = false;
    
    // 隐藏tabbar
    self.tabBarController.tabBar.hidden = true;
    
    // 添加地图视图
    [self.view addSubview:self.mapView];
    
    // 添加位置按钮
    [self.view addSubview:self.locationBtn];
    
    
    // 添加底部视图
    self.shopNameLabel.frame = CGRectMake(Size_W(8), 0, ScreenW - Size_W(16), Size_H(30));
    self.shopNameLabel.text = self.shopName;
    [self.shopNameLabel sizeToFit];
    
    self.shopAddressLabel.frame = CGRectMake(Size_W(8), CGRectGetMaxY(self.shopNameLabel.frame), ScreenW - Size_W(16), Size_H(30));
    self.shopAddressLabel.text = self.shopAddress;
    [self.shopAddressLabel sizeToFit];
    
    self.bottomView = [[UIView alloc] init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.shopNameLabel];
    [self.bottomView addSubview:self.shopAddressLabel];
    
    self.bottomView.frame = CGRectMake(0, ScreenH - CGRectGetMaxY(self.shopAddressLabel.frame) - Size_H(10), ScreenW, CGRectGetMaxY(self.shopAddressLabel.frame) + Size_H(10));
    
    [self.view addSubview:self.bottomView];
    
    // 重设按钮和地图的位置
    self.locationBtn.center = CGPointMake(self.locationBtn.center.x, self.locationBtn.center.y - CGRectGetHeight(self.bottomView.bounds));
    
    self.mapView.frame = CGRectMake(0, self.mapView.frame.origin.y, ScreenW, self.mapView.frame.size.height - CGRectGetHeight(self.bottomView.bounds));
}

#pragma mark -- CLLocationManagerDelegate
// 定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    // 打印错误信息
    NSLog(@"%@", error.localizedDescription);
}

// 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
}

#pragma mark -- MKMapViewDelegate
// 地图加载失败
- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error {
    
}

// 用户位置更新
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
}

// 自定义标注视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    // 异常处理
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
//    
//    static NSString *identifier = @"111";
//    
//    // 重用
//    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
//    
//    if (!pinView) {
//        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
//    }
//    
//    // 重新设置大头针的标注数据源
//    pinView.annotation = annotation;
//    
//    // 设置凋零效果
//    pinView.animatesDrop = true;
//    
//    // 设置是否允许弹窗
//    pinView.canShowCallout = true;
//    
//    // 修改大头针的颜色
//    pinView.pinTintColor = [UIColor redColor];
//
//    return pinView;
    
    MKAnnotationView *newAnnotation=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
    if ([[annotation title] isEqualToString:@"我的位置"]) {
//        newAnnotation.image = [UIImage imageNamed:@"person_location"];
    }
    else{
        newAnnotation.image = [UIImage imageNamed:@"定位箭头"];
        
    }
    newAnnotation.canShowCallout=YES;
    return newAnnotation;
    
}

#pragma mark -- 监听事件的回调方法
// 点击按钮地图回到用户的当前位置
- (void)respondsToLoactionBtn:(UIButton *)sender {
    if (self.mapView.userTrackingMode != MKUserTrackingModeFollow) {
        [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:true];
    }
}

#pragma mark -- Getter 
- (MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        // 是否呈现用户当前位置
        _mapView.showsUserLocation = true;
        // 设置用户跟踪类型
//        _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
        
        // 设置地图样式
        _mapView.mapType = MKMapTypeStandard;
        
        // 是否显示交通、指南针、比例尺
        _mapView.showsTraffic = true;
        _mapView.showsCompass = true;
        _mapView.showsScale = true;
        
        // 设置标题
        _mapView.userLocation.title = @"我的位置";
        
        // 设置子标题
//        _mapView.userLocation.subtitle = @"";
        
        // 设置代理
        _mapView.delegate = self;
        
    }
    
    return _mapView;
}

- (UIButton *)locationBtn {
    if (!_locationBtn) {
        _locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _locationBtn.frame = CGRectMake(Size_W(10), CGRectGetMaxY(self.mapView.frame) - Size_H(50), Size_W(40), Size_H(40));
        
        _locationBtn.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
        [_locationBtn setImage:[UIImage imageNamed:@"位置按钮.png"] forState:UIControlStateNormal];
        
        [_locationBtn addTarget:self action:@selector(respondsToLoactionBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _locationBtn;
}

- (UILabel_Font *)shopNameLabel {
    if (!_shopNameLabel) {
        _shopNameLabel = [[UILabel_Font alloc] init];
        _shopNameLabel.font = [UIFont boldSystemFontOfSize:Font(17)];
    }
    
    return _shopNameLabel;
}

- (UILabel_Font *)shopAddressLabel {
    if (!_shopAddressLabel) {
        _shopAddressLabel = [[UILabel_Font alloc] init];
        
        _shopAddressLabel.textColor = [UIColor lightGrayColor];
        _shopAddressLabel.font = [UIFont boldSystemFontOfSize:Font(14)];
        _shopAddressLabel.numberOfLines = 0;
    }
    
    return _shopAddressLabel;
}

@end
